package com.xiangban.yanglaodajie_platform.controller;


import com.xiangban.yanglaodajie_platform.aop.Permission;
import com.xiangban.yanglaodajie_platform.entity.ShoppingCart;

import com.xiangban.yanglaodajie_platform.entity.dto.OrderNoDto;
import com.xiangban.yanglaodajie_platform.entity.res.ApiResult;
import com.xiangban.yanglaodajie_platform.service.ShoppingCartService;
import com.xiangban.yanglaodajie_platform.utils.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author Mr. MaYaYun
 * @Data 2021/3/17 16:15
 */
@RestController
@RequestMapping("/api/shopping_cart")
@Api(tags = "（软件）购物车")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/getPage")
    @ApiOperation("获取购物车信息")
    @Permission
    public JsonData getPage(@RequestBody ShoppingCart shoppingCart){
       return JsonData.buildSuccess(shoppingCartService.getPage(shoppingCart),"操作成功");
    }

    @PostMapping("/saveData")
    @ApiOperation("增加或减少购物车数量")
    @Permission
    public ApiResult<Object> saveData(@RequestBody ShoppingCart shoppingCart){
        shoppingCartService.saveAndUpdate(shoppingCart);
        return ApiResult.newSuccess();
    }

    @PostMapping("/getSettlement")
    @ApiOperation("未支付订单结算")
    @Permission
    public JsonData getSettlement(@RequestBody OrderNoDto orderNoDto){
        int settlement = shoppingCartService.Settlement(orderNoDto);
        if (settlement==-1){
            return JsonData.buildError("订单状态不同 ");
        }
        return settlement == -2 ? JsonData.buildError("支付失败，积分不足") :JsonData.buildSuccess("支付成功");
    }

    @PostMapping("/clearAllByShopId")
    @ApiOperation("根据商家id清空对应购物车里商家下的商品")
    public ApiResult<Object> clearAllByShopId(@RequestBody ShoppingCart shoppingCart){
        shoppingCartService.clearAll(shoppingCart);
        return ApiResult.newSuccess();
    }

}
